<template>
  <div class="formDiv">
    <el-card class="box-card">
      <div class="back">
        <div>
          <el-button
            class="submission priamry_border"
            style="display: flex; float: right"
            @click="back()"
            >返回</el-button
          >
        </div>
      </div>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        clearable
        label-width="102px"
        :rules="rules"
        class="formArea"
      >
        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 酒店名称 -->
              <el-form-item label="酒店名称" prop="jdName">
                <el-select v-model="ruleForm.jdName" placeholder="请选择酒店">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.jdName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 房间号 -->
              <el-form-item label="房间号" prop="fjNumber">
                <el-input v-model="ruleForm.fjNumber"></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>

        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 房型 -->
              <el-form-item label="房型" prop="fxName">
                <el-select
                  v-model="ruleForm.fxName"
                  clearable
                  placeholder="请选择房型"
                  @change="changeJdName($event)"
                >
                  <el-option
                    v-for="item in options4"
                    :key="item.fxId"
                    :label="item.fxName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 房间名称 -->
              <el-form-item label="房间名称" prop="fjName">
                <el-input v-model="ruleForm.fjName" disabled></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>

        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 房间wifi名称 -->
              <el-form-item
                label="wifi 名称"
                prop="fjWifiName"
                class="roomStatus"
              >
                <el-select
                  v-model="ruleForm.fjWifiName"
                  clearable
                  placeholder="请选择wifi名称"
                  @change="toWifiPwd($event)"
                >
                  <el-option
                    v-for="item in options5"
                    :key="item.wifiName"
                    :label="item.wifiName"
                    :value="item.wifiName"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 房间wifi密码 -->
              <el-form-item label="房间wifi密码" prop="fjWifiPwd">
                <el-input v-model="ruleForm.fjWifiPwd" disabled></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>

        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 房间价格 -->
              <el-form-item label="房间价格" prop="fjPrice">
                <el-input v-model="ruleForm.fjPrice"></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 房间楼层 -->
              <el-form-item label="房间楼层" prop="fjFloor">
                <el-input v-model="ruleForm.fjFloor"></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>

        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 房间密码 -->
              <el-form-item label="房间密码" prop="fjPwd">
                <el-input v-model="ruleForm.fjPwd"></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 房间是否展示 -->
              <el-form-item label="房间展示" prop="isShow">
                <el-tooltip
                  effect="dark"
                  :content="ruleForm.isShow == '0' ? '不展示' : '展示'"
                  placement="right"
                >
                  <el-switch
                    v-model="ruleForm.isShow"
                    :active-value="1"
                    :inactive-value="0"
                  >
                  </el-switch>
                </el-tooltip>
              </el-form-item></div
          ></el-col>
        </el-row>
        <div class="picUpload">
          <!-- 房间图片 -->
          <el-form-item label="房间图片：">
            <div class="upLoadPicBox">
              <div
                v-for="(item, index) in ruleForm.fjImg"
                :key="index"
                class="pictrue"
                draggable="true"
              >
                <img :src="item" />
                <i
                  v-if="!isDisabled"
                  class="el-icon-error btndel"
                  @click="handleRemove(index)"
                />
              </div>
              <div
                v-if="ruleForm.fjImg != [] && !isDisabled"
                @click="modalPicTap('2')"
              >
                <div class="upLoad">
                  <i class="el-icon-camera cameraIconfont" />
                </div>
              </div>
            </div>
          </el-form-item>
        </div>
        <el-form-item ref="editbutton" class="buttons">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >保存</el-button
          >
          <el-button @click="cancel()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {
  getRoomApi,
  groggeryListApi,
  editRoomApi,
  selectRoomTypeApi,
} from "@/api/roomInformation";
import { wifiListApi } from "@/api/wifiType";
export default {
  created() {
    this.getRoom(); // 根据房间 id 查询房间
    this.gethotalList(); // 获取所有的酒店
    this.getRoomTypeList(); //获取所有的房型
    this.loadData(); //获取wifi列表
  },
  data() {
    return {
      isDisabled: this.$route.params.isDisabled === "1" ? true : false,
      options5: [], //wifi列表
      //入住状态
      options2: [
        {
          status: 0,
          label: "未入住",
        },
        {
          status: 1,
          label: "已入住",
        },
      ],
      //查询所有酒店
      hotalForm: {
        limit: 10,
        page: 1,
      },
      id: "",
      options: [], //所有酒店名称
      options4: [], //房型数据
      //表单
      ruleForm: {
        fxName: "", //房型名称
        fxId: "",
        fjFloor: "", //房间楼层
        jdName: "", //酒店名称
        fjName: "", //房间名称
        fjNumber: "", //房间号
        fjPrice: "", //房间价格
        fjPwd: "", //房间密码
        fjWifiName: "", //房间wifi名称
        fjWifiPwd: "", //房间wifi密码
        fjImg: [], //房间图片
        jdId: "", //酒店id
        fjWifiId: 0, //房间 wifi Id
        id: 0,
        isDel: 0,
        isShow: "", // 房间是否显示
        sort: 0,
        status: "", //酒店状态
      },
      rules: {
        //房间是否显示
        isShow: [
          { required: true, message: "请选择房间是否显示", trigger: "change" },
        ],
        //房间楼层
        fjFloor: [
          { required: true, message: "请输入房间楼层", trigger: "blur" },
        ],
        //房型
        fxName: [{ required: true, message: "请输入房型", trigger: "blur" }],
        //房间密码
        fjPwd: [{ required: true, message: "请输入房间密码", trigger: "blur" }],

        //房间wifi名称
        fjWifiName: [
          { required: true, message: "请输入房间wifi名称", trigger: "blur" },
        ],
        //房间wifi密码
        fjWifiPwd: [
          { required: true, message: "请输入房间wifi密码", trigger: "blur" },
        ],
        //酒店房间名称
        fjName: [
          { required: true, message: "请选择房间名称", trigger: "blur" },
        ],
        //房间号
        fjNumber: [
          { required: true, message: "请填写房间号", trigger: "blur" },
        ],
        //房间价格
        fjPrice: [{ required: true, message: "请填写价格", trigger: "blur" }],
        //房间状态
        status: [
          { required: true, message: "请输入房间状态", trigger: "change" },
        ],
        //酒店名称
        jdName: [
          { required: true, message: "请输入酒店名称", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    // //回显 wifi 密码
    toWifiPwd(e) {
      let result1 = this.options5.find((item) => e == item.wifiName);
      this.ruleForm.fjWifiPwd = result1.password;
    },
    //  回显房型名称
    changeJdName(val) {
      this.ruleForm.fxId = val;
      let result = this.options4.find((item) => val == item.id);
      this.ruleForm.fjName = result.fxName;
    },
    // 上传房间图片
    modalPicTap(tit, num) {
      const _this = this;
      if (_this.isDisabled) return;
      _this.$modalUpload(
        function (img) {
          if (tit === "2" && !num) {
            if (img.length >= 10)
              return this.$message.warning("最多选择10张图片！");
            if (img.length + _this.ruleForm.fjImg.length > 10)
              return this.$message.warning("最多选择10张图片！");
            img.map((item) => {
              _this.ruleForm.fjImg.push(item.sattDir);
            });
          }
        },
        tit,
        "content"
      );
    },
    //删除选择的房间图片
    handleRemove(i) {
      this.ruleForm.fjImg.splice(i, 1);
    },
    //返回上一页
    back() {
      this.$router.push("/amszs/msroom/index");
    },
    // 获取 wifi 列表
    async loadData() {
      const res = await wifiListApi(this.pagesize);
      this.options5 = res.list;
      this.total = res.total;
    },
    //获取所有的房型
    async getRoomTypeList() {
      const res = await selectRoomTypeApi(this.hotalForm);
      this.options4 = res.list;
    },
    // 2.根据房间 id 查询房间
    async getRoom() {
      const id = JSON.parse(sessionStorage.getItem("data"));
      const res = await getRoomApi({ id });
      this.ruleForm = res;
    },
    //获取酒店列表
    async gethotalList() {
      const res = await groggeryListApi(this.hotalForm);
      this.options = res.list;
    },
    //修改房间
    submitForm() {
      //兜底校验
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return;
        this.ruleForm.status = Number(this.ruleForm.status);
        // //通过后发请求
        await editRoomApi(this.ruleForm);
        this.$message.success("修改成功");
        this.$router.push("/amszs/msroom/index");
      });
    },
    //取消按钮
    cancel() {
      this.$router.push("/amszs/msroom/index");
    },
  },
};
</script>

<style lang="scss" scoped>
.formDiv {
  margin-top: 40px;
  margin-left: 30px;
  .back {
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: end;
  }
  .formArea {
    padding: 0 30px;
    .picUpload {
      width: 800px;
      height: 100px;
      // background-color: pink;
      .upLoadPicBox {
        display: flex;
        width: 800px;
        height: 70px;
        // background-color: blue;
      }
    }
    .pictrue {
      width: 60px;
      height: 60px;
      border: 1px dotted rgba(0, 0, 0, 0.1);
      margin-right: 10px;
      position: relative;
      cursor: pointer;
    }
    .btndel {
      position: absolute;
      z-index: 1;
      width: 20px !important;
      height: 20px !important;
      left: 46px;
      top: -4px;
    }
  }
  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-input__inner {
    width: 660px;
  }
  .el-select .el-select--medium {
    width: 660px;
  }
  .buttons {
    width: 100%;
    height: 50px;
  }
}
.upload {
  width: 80px;
  height: 80px;
  // display: flex;
  text-align: center;
  line-height: 80px;
  // background-color: pink;
  border: 1px solid #fff;
}
</style>